package com.example.compete.service.impl;

import com.example.compete.DTO.CompanyAndExecutiveDTO;
import com.example.compete.DTO.CompanyDTO;
import com.example.compete.DTO.DocumentTypeDTO;
import com.example.compete.DTO.ExecutiveDTO;
import com.example.compete.mapper.CompanyMapper;
import com.example.compete.mapper.ExecutiveMapper;
import com.example.compete.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;
    
    private CompanyDTO companyDTO;
    
    @Autowired
    private ExecutiveMapper executiveMapper;

    //查找所有公司信息
    @Override
    public List<CompanyDTO> findAll() {
        List<CompanyDTO> companyDTOList= companyMapper.findAllCompany();
        for(CompanyDTO companyDTO:companyDTOList) {
        	String compString=companyMapper.selectTypeName(companyDTO.getDocumentType());
        	companyDTO.setDocumentName(compString);
        }
        return companyDTOList;
    }

    //根据公司编号查询信息
    @Override
    public CompanyDTO findByCompanyId(String companyId) {
        return companyMapper.findByCompanyId(companyId);
    }

    //修改公司信息
    @Override
    public void updateByCompanyId(CompanyDTO companyDTO) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        companyDTO.setUpdateTime(simpleDateFormat.format(new Date()));
        companyMapper.updateByCompanyId(companyDTO);
    }
    
    //添加公司信息
    @Override
    public CompanyDTO add(CompanyDTO companyDTO) {

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<CompanyDTO> companyList=companyMapper.findAllCompany();
        String lastCompanyId=null;
        String count=null;
        if(companyList.size()==0){
            lastCompanyId="C0000000000";
        }else{
            lastCompanyId=companyList.get(companyList.size()-1).getCompanyId();
        }
        char[] companyIdChar=lastCompanyId.toCharArray();
        companyIdChar[0]='0';
        count=String.valueOf(companyIdChar);
        int countInt=Integer.valueOf(count);
        StringBuffer countStringBuffer=new StringBuffer(String.valueOf(countInt+1));
        while(true){
            if(countStringBuffer.length()==10){
                break;
            }else{
                countStringBuffer.insert(0, '0');
            }
        }
        countStringBuffer.insert(0, 'C');
        companyDTO.setCompanyId(countStringBuffer.toString());
        companyDTO.setRegisteredTime(simpleDateFormat.format(new Date()));
        companyDTO.setUpdateTime(simpleDateFormat.format(new Date()));
        companyMapper.add(companyDTO);
        List<CompanyDTO> companyList1=companyMapper.findAllCompany();
        CompanyDTO cpmpanyDTO=companyList1.get(companyList1.size()-1);
        String compString=companyMapper.selectTypeName(companyDTO.getDocumentType());
    	companyDTO.setDocumentName(compString);
    	return companyDTO;
    }


    //查找证件信息
	@Override
	public List<DocumentTypeDTO> findAllDocument() {
		// TODO Auto-generated method stub
		return companyMapper.findAllDocumentType();
	}

	//关联删除公司和高管信息
	@Override
	public void deleteCompanyAndExecutive(String companyId) {
		if(companyMapper.findExecutive(companyId).size()>0) {
			 companyMapper.deleteCompanyAndExecutive(companyId);
		}else {
			companyMapper.delectOneCompany(companyId);
		}
	   
	}
	
	//根据公司编号查找公司和高管信息
	@Override
    public CompanyAndExecutiveDTO selectCompanyAndExecutiveByCompanyId(String companyId) {
		CompanyAndExecutiveDTO companyAndExecutiveDTO = new CompanyAndExecutiveDTO();
		CompanyDTO companyDTO=companyMapper.selectOneCompanyById(companyId);
		String compString=companyMapper.selectTypeName(companyDTO.getDocumentType());
    	companyDTO.setDocumentName(compString);
		companyAndExecutiveDTO.setCompanies(companyDTO);
		List<ExecutiveDTO> executiveDTOs=executiveMapper.selectAllByCompanyId(companyId);
		 for (ExecutiveDTO executiveDTO:executiveDTOs) {
	            String postName = executiveMapper.findPostName(executiveDTO.getPost());
	            String sexName = executiveMapper.findSexName(executiveDTO.getSex());
	            String educationName = executiveMapper.findEducationName(executiveDTO.getEducation());
	            executiveDTO.setPostName(postName);
	            executiveDTO.setSexName(sexName);
	            executiveDTO.setEducationName(educationName);
	        }
		companyAndExecutiveDTO.setExecutives(executiveDTOs);
	    return companyAndExecutiveDTO;
	}

	//检查信息是否重复
	@Override
	public List<CompanyDTO> checkCompany(CompanyDTO companyDTO) {
		// TODO Auto-generated method stub
		return companyMapper.checkCompany(companyDTO);
	}


}
